<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-06-22 23:11:25 +0200 (Sun, 22 Jun 2008) $
 * $Rev: 1212 $
 * $Author: styck $
 * $Id: install.6.inc.php 1212 2008-06-22 21:11:25Z styck $
 */

// This is an include-only file:
if (!defined('ORIGINAL_PAGE')) die("This file cannot be accessed directly.");


if(!function_exists('echo_debug')) { function echo_debug(){}; }
if(!function_exists('echo_debug_function')) { function echo_debug_function(){}; }
if(!function_exists('write_log')) { function write_log(){}; }

// Finish up
$urduser = $_REQUEST['urduser'];
$urdpass1 = $_REQUEST['urdpass1'];
$urdpass2 = $_REQUEST['urdpass2'];
$urdfullname = $_REQUEST['urdfullname'];
$urdemail = $_REQUEST['urdemail'];
$urddownloaddir = $_REQUEST['urddownloaddir'];
$urdupdate = isset($_REQUEST['urdupdatecheck']) ? TRUE : FALSE;

// Store in session:
$_SESSION['urdusername'] = $urduser;
$_SESSION['urdusermail'] = $urdemail;
$_SESSION['urduserfull'] = $urdfullname;
$_SESSION['urduserpass1'] = $urdpass1;
$_SESSION['urduserpass2'] = $urdpass2;
$_SESSION['urdupdatecheck'] = $urdupdate;
$_SESSION['urddownloaddir'] = $urddownloaddir;

get_urdd_path();
get_url();

// This SHOULD work now:
$OUT .= '<tr><td class="install2">Connecting to db using stored settings</td>';
try{
	require_once('../functions/db.class.php');
	require_once('../functions/functions.php');
	require_once('../functions/defaults.php');
	$db = connect_db();
	$rv_cdb = TRUE;
} catch (exception $e) {
	$rv_cdb = FALSE;
	$error = $e->getMessage();
}
$OUT .= GenRetVal($rv_cdb);
if ($rv_cdb == FALSE)
	$OUT .= ShowHelp($error);


// Checking user/password:
$OUT .= '<tr><td class="install2">Username valid</td>';
try {
    $rv_un = valid_username($urduser, 2);
} catch (exception $e) {
    $rv_un = FALSE;
	$error = $e->getMessage(). '. ';
} 

$OUT .= GenRetVal($rv_un === TRUE,$rv_uv);
if (!$rv_uv) $OUT .= ShowHelp("{$error}Please enter a username ('root' is unacceptable). Only letters, numbers and underscore is accepted, minimally 3 characters");

$OUT .= '<tr><td class="install2">Fullname valid</td>';
$OUT .= GenRetVal($urdfullname != '',$rv_ufn);
if (!$rv_ufn) $OUT .= ShowHelp("Please enter your full name");

$OUT .= '<tr><td class="install2">Email valid</td>';
$OUT .= GenRetVal(verify_email($urdemail),$rv_uem);
if (!$rv_uem) $OUT .= ShowHelp("Please enter a valid email address");

$OUT .= '<tr><td class="install2">Both passwords the same</td>';
$OUT .= GenRetVal($urdpass1 == $urdpass2,$rv_ps);

$OUT .= '<tr><td class="install2">Password acceptable</td>';
$rv_pe = verify_password($urdpass1, $urduser);
$OUT .= GenRetVal($rv_pe === TRUE,$rv_pex);
if (!$rv_pe) $OUT .= ShowHelp($rv_pe);

$OUT .= '<tr><td class="install2">Download directory</td>';
$rv_dd = is_dir($urddownloaddir) && is_writable($urddownloaddir);
$OUT .= GenRetVal($rv_dd === TRUE,$rv_dd);
if (!$rv_dd) $OUT .= ShowHelp("Download directory not found or not writeable. ");


$OUT .= '<tr><td class="install2">Creating URD user account</td>';
try{
	if ($rv_cdb === FALSE) 
		throw new exception("Database connection required");
	if (!$rv_pex || !$rv_ps || !$rv_uem || !$rv_ufn || !$rv_uv) 
		throw new exception("Valid data required");
	// Make user with admin rights:
	add_user($db, $urduser, $urdfullname, $urdemail, $urdpass1, USER_ADMIN, USER_ACTIVE, 'CRUDPA'); // TODO add user full name and email address
    set_config($db, 'run_update', $urduser); // run updategroups at startup as user just created
    set_config($db, 'dlpath', $urddownloaddir);
    set_config($db, 'admin_email', $urdemail);
    set_config($db, 'urdd_path', $_SESSION['urdd']);
    set_config($db, 'url', $_SESSION['url']);
	$rv_cu = TRUE;
} catch (exception $e) {
	$rv_cu = FALSE;
	$error = $e->getMessage();
}
$OUT .= GenRetVal($rv_cu);
if ($rv_cu === FALSE)
	$OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';

// schedule version checks:
$rv_uu = TRUE;
if ($urdupdate) {
	$OUT .= '<tr><td class="install2">Scheduling weekly version checks</td>';
	try{
		if ($rv_cdb === FALSE) 
			throw new exception("Database connection required");
		if (!$rv_pex || !$rv_ps || !$rv_uem || !$rv_ufn || !$rv_uv || !$rv_cu)
			throw new exception("Valid data required");

		add_schedule($db, 'CHECK_VERSION',1205060400,604800,$urduser);
		$rv_uu = TRUE;
	} catch (exception $e) {
		$rv_uu = FALSE;
		$error = $e->getMessage();
	}
	$OUT .= GenRetVal($rv_uu);
	if ($rv_uu === FALSE)
		$OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';
}
// Config file:
$OUT .= '<tr><td class="install2">Generating config file</td>';
try{
	if (!$rv_pex || !$rv_ps || !$rv_uem || !$rv_ufn || !$rv_uv) throw new exception("Valid data required");
	exec("cp config.php.default ../config.php");
	$rv_ccf = TRUE;
} catch (exception $e) {
	$rv_ccf = FALSE;
	$error = $e->getMessage();
}
$OUT .= GenRetVal($rv_ccf);
if ($rv_ccf === FALSE)
	$OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';

// Finish up!
if ($rv_ccf && $rv_cu && $rv_cdb && $rv_pe && $rv_ps && $rv_uv && $rv_uem && $rv_ufn && $rv_uu && $rv_dd) {

	// Generate a .installed file, and check for that at the start (die if exists)
	// to prevent hackers from creating a clean install with default password or such.
	$OUT .= '<tr><td class="install2">Finishing installation by generating .installed file</td>';
	try{
		// Doesn't really need to be a try/catch; touch returns true or false when failed.
        $rv_cif = touch("../.installed");
        file_put_contents("../.installed", "URD " . get_version() . "\n" . date ('c') . "\n");
	} catch (exception $e) {
		$rv_cif = FALSE;
		$error = $e->getMessage();
	}
	$OUT .= GenRetVal($rv_cif);
	if ($rv_cif === FALSE)
		$OUT .= '<tr colspan="2"><td><span class="info">' . $error . '</span></td></tr>';

	// Tell user to start urdd after setting the preferences
	$OUT .= '<tr colspan="2"><td><h2>Congratulations!</h2>It seems the installation has completed successfully. Please 
		continue to the configuration page to enter the settings, and when complete start the URD Daemon (either through the website or via ./urdd.sh).
		<br/>&nbsp;<br/><br/><a href="../html/admin_usenet_servers.php">Continue to the usenet server configuration</a></td></tr>
		<tr colspan="2"><td>Please take a moment to register an account at our forum as well. This will enable the feature to automatically retrieve additional information about downloads: 
		<a href="http://www.urdland.com/forum">Go to the forum</a></td></tr>';

} else {
	$OUT .= '<tr colspan="2"><td>Please try to correct any error you might see.</td></tr>';
	$OUT .= '<tr colspan="2"><td><a onclick="LoadPage(5);">'.$refreshpic.'</a></td></tr>';
}
?>
